package cn.sym.pet.controller;

import cn.sym.basic.exception.MyException;
import cn.sym.basic.jwt.LoginData;
import cn.sym.basic.util.AjaxResult;
import cn.sym.basic.util.LoginContext;
import cn.sym.basic.util.PageList;
import cn.sym.org.domain.Employee;
import cn.sym.pet.domain.SearchMasterMsg;
import cn.sym.pet.dto.HandleMsgDto;
import cn.sym.pet.query.SearchMasterMsgQuery;
import cn.sym.pet.service.ISearchMasterMsgService;
import cn.sym.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg  searchMasterMsg, HttpServletRequest request) {
        try {
            LoginData loginData = (LoginData) LoginContext.getLogin(request);
            System.out.println(loginData);
            User user = loginData.getUser();
            searchMasterMsg.setUserId(user.getId());
            searchMasterMsgService.publish(searchMasterMsg,user);
            return AjaxResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，请稍后再试！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.remove(id);
            return AjaxResult.my();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id) {
        return searchMasterMsgService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 前端用户查询自己发布的寻主消息----个人中心
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> myMsg(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
       User user = (User) LoginContext.getLogin(request);
       query.setUserId(user.getId());
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 待审核
     * @param searchMasterMsgQuery
     * @return
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery){
        return searchMasterMsgService.queryPage(searchMasterMsgQuery);
    }

    /**
     * 待处理
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        query.setState(1);
        LoginData loginData = (LoginData) LoginContext.getLogin(request);
        Employee employee = loginData.getAdmin();
        query.setShopId(employee.getShop_id());
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 寻主池
     * @param query
     * @return
     */
    @PostMapping("/toSearchMaster")
    public PageList<SearchMasterMsg> toSearchMaster(@RequestBody SearchMasterMsgQuery query){
        query.setState(1);
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 完成
     * @param query
     * @return
     */
    @PostMapping("/toSuccess")
    public PageList<SearchMasterMsg> toSuccess(@RequestBody SearchMasterMsgQuery query){
        query.setState(2);
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 接单
     * @param handleMsgDto
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody HandleMsgDto handleMsgDto){
        try {
            searchMasterMsgService.accept(handleMsgDto);
            return AjaxResult.my();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("接单失败！"+e.getMessage());
        }

    }

    /**
     * 拒单
     * @param msgId
     * @return
     */
    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId){
        try {
            searchMasterMsgService.reject(msgId);
            return AjaxResult.my();
        } catch (MyException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }



}
